﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using UBCon.Core.Commands;

namespace UBCon.Controls
{
    public class WaterMarkTextBox : TextBox
    {
        //------------------------------------------------------
        //
        //  Constructors
        //
        //------------------------------------------------------
        #region Constructors

        /// <summary>
        /// Initializes a new instance of UBCon.Controls.WaterMarkTextBox class.
        /// </summary>
        public WaterMarkTextBox()
            : base()
        {
            _clearTextCommand = new Command(OnClearCommandExecuted);
            SetValue(ClearTextCommandProperty, _clearTextCommand);
        }

        static WaterMarkTextBox() 
        {
            Type ownerType=typeof(WaterMarkTextBox);
            DefaultStyleKeyProperty.OverrideMetadata(ownerType, new FrameworkPropertyMetadata(ownerType));
        }
        #endregion

        //------------------------------------------------------
        //
        //  Properties
        //
        //------------------------------------------------------
        #region Properties

        /// <summary>
        /// Gets or sets watermark text for the UBCon.Controls.WaterMarkTextBox control.
        /// </summary>
        public string WatermarkText
        {
            get { return (string)GetValue(WatermarkTextProperty); }
            set { SetValue(WatermarkTextProperty, value); }
        }

        /// <summary>
        /// Identifies the UBCon.Controls.WaterMarkTextBox.WatermarkText dependency property.
        /// </summary>
        public static readonly DependencyProperty WatermarkTextProperty =
            DependencyProperty.Register("WatermarkText", typeof(string), typeof(WaterMarkTextBox),
            new FrameworkPropertyMetadata((string)null, 
                FrameworkPropertyMetadataOptions.Journal | FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

        /// <summary>
        /// Gets or sets a boolean value that indicates whether WaterMarkTextBox control has text or not.
        /// </summary>
        public bool HasText
        {
            get { return (bool)GetValue(HasTextProperty); }
            set { SetValue(HasTextProperty, value); }
        }

         /// <summary>
        /// Identifies the UBCon.Controls.WaterMarkTextBox.HasText dependency property.
        /// </summary>
        public static readonly DependencyProperty HasTextProperty =
            DependencyProperty.Register("HasText", typeof(bool), typeof(WaterMarkTextBox), new FrameworkPropertyMetadata(false));

        /// <summary>
        /// Gets or sets a command that clears text.
        /// </summary>
        public ICommand ClearTextCommand
        {
            get { return (ICommand)GetValue(ClearTextCommandProperty); }
            set { SetValue(ClearTextCommandProperty, value); }
        }

         /// <summary>
        /// Identifies the UBCon.Controls.WaterMarkTextBox.ClearTextCommand dependency property.
        /// </summary>
        public static readonly DependencyProperty ClearTextCommandProperty =
            DependencyProperty.Register("ClearTextCommand", typeof(ICommand), typeof(WaterMarkTextBox), new FrameworkPropertyMetadata((ICommand)null));
        
        #endregion

        //------------------------------------------------------
        //
        //  Depenency Property Changed Callbacks
        //
        //------------------------------------------------------
        #region Depenency Property Changed Callbacks
        #endregion

        //------------------------------------------------------
        //
        //  Overrides
        //
        //------------------------------------------------------
        #region Overrides

        /// <summary>
        /// Invoked when TextBox control raises TextChanged event.
        /// </summary>
        protected override void OnTextChanged(TextChangedEventArgs e)
        {
            base.OnTextChanged(e);

            SetValue(HasTextProperty, !string.IsNullOrEmpty(Text));
        }
        #endregion

        //------------------------------------------------------
        //
        //  Virual members
        //
        //------------------------------------------------------
        #region Virual members

        #endregion

        //------------------------------------------------------
        //
        //  Public Methods
        //
        //------------------------------------------------------
        #region Public Methods
        #endregion

        //------------------------------------------------------
        //
        //  Internal Methods
        //
        //------------------------------------------------------
        #region Internal Methods
        #endregion

        //------------------------------------------------------
        //
        //  Private Methods
        //
        //------------------------------------------------------
        #region Private Methods

        /// <summary>
        /// Invoked when _clearTextCommand is executed.
        /// </summary>
        private void OnClearCommandExecuted(object parameter) 
        {
            SetValue(TextProperty, null);
        }

        #endregion

        //------------------------------------------------------
        //
        //  Event Handlers
        //
        //------------------------------------------------------
        #region Event Handlers
        #endregion

        //------------------------------------------------------
        //
        //  Fields
        //
        //------------------------------------------------------
        #region Fields
        #endregion

        //------------------------------------------------------
        //
        //  Events
        //
        //------------------------------------------------------
        #region Events
        #endregion

        //------------------------------------------------------
        //
        //  Commands
        //
        //------------------------------------------------------
        #region Commands

        private Command _clearTextCommand;
        #endregion

    }
}
